<template>
	<view class="container">
		首页
		
	</view>
</template>

<script>
import store from '@/store';
import { mapState } from 'vuex';
import { callContainerApi } from '@/api/callContainerApi.js'
export default {
	computed: {
		...mapState(['isLoggedIn'])
	},
	data() {
		return {};
	},
	methods: {
		requestToServer(code) {
			wx.cloud.init()
			
			callContainerApi('/api/count', {"action": "inc","code":code}, 'POST').then((result) => {
			  console.log("调用结果:", result.data);
			  	store.commit('login');
				// uni.setStorageSync('userInfo', res.data.userInfo);
				// uni.redirectTo({ url: '/pages/index/index.vue' });
			}).catch((error) => {
			  console.error("调用出错:", error);
			});
		}
	},
	created() {
		console.log('isLoggedIn', this.isLoggedIn);
		if (!this.isLoggedIn) {
			uni.login({
				provider: 'weixin',
				success: res => {
					console.log(res);
					// res.code 是微信登录返回的临时凭证
					if (res.code) {
						// 通常这里需要将 code 发送到后端服务器，由服务器与微信服务器进行交互获取用户信息
						this.requestToServer(res.code);
					} else {
						console.log('微信登录失败：', res.errMsg);
					}
				},
				fail: err => {
					console.log('微信登录错误：', err);
				}
			});
		}
	}
};
</script>

<style>
.container {
	padding: 20px;
	font-size: 14px;
	line-height: 24px;
}
</style>
